
####
## Candidate identity and campaign priming: Analyzing voter support for Pete Buttigieg's presidential run as an openly gay man
####



### Load libraries
library(arm)
library(stargazer)
library(sjPlot)
library(ggplot2)
library(ggthemes)
library(foreign)
library(mediation)
library(cregg)
library(FindIt)
library(interplot)


library(plyr)


### Read and attach data


pete.trump <- read.csv("C:/Users/gmagni/OneDrive/SideProjects/9-Andy-Pete/7-PRQ/FinalVersion-AfterR&R/Submit/Magni-Reynolds_PeteButtigieg_PRQ-data.csv")



###############

## Subsets 
## Only needed for TABLE 3 and TABLE A7: analysis of treatment effect by subgroups - otherwise skip this section

# party ID
pete.trump <- subset(pete.trump, partyID == "Democrat")
pete.trump <- subset(pete.trump, partyID == "Republican")
pete.trump <- subset(pete.trump, partyID == "Independent")


#ideology
pete.trump <- subset(pete.trump, ideol == "Moderate or middle of the road")


#race
pete.trump <- subset(pete.trump, race == "White")
pete.trump <- subset(pete.trump, race=="Black or African American" | race=="Black or African American,Other" | race=="Black or African American,White")


#LGBTQ friends
pete.trump <- subset(pete.trump, LGBT.friends == "Yes")
pete.trump <- subset(pete.trump, LGBT.friends == "No")


#LGBTQ
pete.trump <- subset(pete.trump, Sex.or == "Gay" | Sex.or == "Lesbian" | Sex.or == "Bisexual/pansexual" | Sex.or == "Other"
                       | (gender == "Transgender female" & Sex.or == "Straight") | (gender == "Transgender male" & Sex.or == "Straight")
                       | (gender == "Other" & Sex.or == "Straight")) #LGBTQ
pete.trump <- subset(pete.trump, Sex.or == "Straight") 


#gender binary
pete.trump <- subset(pete.trump, gender == "Female") #cisgender
pete.trump <- subset(pete.trump, gender == "Male") #cisgender

#religiosity
pete.trump <- subset(pete.trump, church == "Never")
pete.trump <- subset(pete.trump, church == "Once a week" | church == "Every day")

#age
age.years<-2020-pete.trump$age
pete.trump <- subset(pete.trump, age.years <= 35)
pete.trump <- subset(pete.trump, age.years >= 36 & age.years <= 59)
pete.trump <- subset(pete.trump, age.years >= 60)





###################

## Subsets 
## Only needed for TABLE A8 - otherwise skip this section

## before Pete DROPPED out (i.e. respondents completing survey before 6 pm on March 1 - N = 4,894)
pete.trump <- read.csv("C:/Users/gmagni/OneDrive/SideProjects/9-Andy-Pete/00-Results/Vote_choicePete_March 5, 2020_7pm-numbers-BEFOREpeteDroppedOut.csv")

## AFTER Pete DROPPED out (i.e. respondents completing survey before 6 pm on March 1 - N = 4,894)
pete.trump <- read.csv("C:/Users/gmagni/OneDrive/SideProjects/9-Andy-Pete/00-Results/Vote_choicePete_March 5, 2020_7pm-numbers-AFTERpeteDroppedOut.csv")

####################




n = nrow(pete.trump)
attach(pete.trump)






### Code

## Re-code treatments to combine treatment groups across different states

pete.neutr <- as.numeric(PeteNeutr.CA == 1 | PeteNeutr.CO == 1 | PeteNeutr.FL == 1 | PeteNeutr.IN == 1 | PeteNeutr.MI == 1 | PeteNeutr.MN == 1 |
                           PeteNeutr.NC == 1 | PeteNeutr.NY == 1 | PeteNeutr.OH == 1 | PeteNeutr.PA == 1 | PeteNeutr.TX == 1 | PeteNeutr.VA == 1)
table(pete.neutr)
pete.trump$pete.neutr <- pete.neutr


pete.gay <- as.numeric(PeteGay.CA == 1 | PeteGay.CO == 1 | PeteGay.FL == 1 | PeteGay.IN == 1 | PeteGay.MI == 1 | PeteGay.MN == 1 |
                         PeteGay.NC == 1 | PeteGay.NY == 1 | PeteGay.OH == 1 | PeteGay.PA == 1 | PeteGay.TX == 1 | PeteGay.VA == 1)
table(pete.gay)
pete.trump$pete.gay <- pete.gay


pete.relig <- as.numeric(PeteRelig.CA == 1 | PeteRelig.CO == 1 | PeteRelig.FL == 1 | PeteRelig.IN == 1 | PeteRelig.MI == 1 | PeteRelig.MN == 1 |
                           PeteRelig.NC == 1 | PeteRelig.NY == 1 | PeteRelig.OH == 1 | PeteRelig.PA == 1 | PeteRelig.TX == 1 | PeteRelig.VA == 1)
table(pete.relig)
pete.trump$pete.relig <- pete.relig


pete.milit <- as.numeric(PeteMilit.CA == 1 | PeteMilit.CO == 1 | PeteMilit.FL == 1 | PeteMilit.IN == 1 | PeteMilit.MI == 1 | PeteMilit.MN == 1 |
                           PeteMilit.NC == 1 | PeteMilit.NY == 1 | PeteMilit.OH == 1 | PeteMilit.PA == 1 | PeteMilit.TX == 1 | PeteMilit.VA == 1)
table(pete.milit)
pete.trump$pete.milit <- pete.milit


pete.proudgay <- as.numeric(PeteSupGay.CA == 1 | PeteSupGay.CO == 1 | PeteSupGay.FL == 1 | PeteSupGay.IN == 1 | PeteSupGay.MI == 1 | PeteSupGay.MN == 1 |
                              PeteSupGay.NC == 1 | PeteSupGay.NY == 1 | PeteSupGay.OH == 1 | PeteSupGay.PA == 1 | PeteSupGay.TX == 1 | PeteSupGay.VA == 1)
table(pete.proudgay)
pete.trump$pete.proudgay <- pete.proudgay


pete.milit.gay <- as.numeric(PeteSGayMilit.CA == 1 | PeteSGayMilit.CO == 1 | PeteSGayMilit.FL == 1 | PeteSGayMilit.IN == 1 | PeteSGayMilit.MI == 1 | 
                               PeteSGayMilit.MN == 1 | PeteSGayMilit.NC == 1 | PeteSGayMilit.NY == 1 | PeteSGayMilit.OH == 1 | PeteSGayMilit.PA == 1 | 
                               PeteSGayMilit.TX == 1 | PeteSGayMilit.VA == 1)
table(pete.milit.gay)
pete.trump$pete.milit.gay <- pete.milit.gay


pete.relig.gay <- as.numeric(PeteSGayRelig.CA == 1 | PeteSGayReli.CO == 1 | PeteSGayRelig.FL == 1 | PeteSGayRelig.IN == 1 | PeteSGayRelig.MI == 1 | 
                               PeteSGayRelig.MN == 1 | PeteSGayRelig.NC == 1 | PeteSGayRelig.NY == 1 | PeteSGayRelig.OH == 1 | PeteSGayRelig.PA == 1 | 
                               PeteSGayRelig.TX == 1 | PeteSGayRelig.VA == 1)
table(pete.relig.gay)
pete.trump$pete.relig.gay <- pete.relig.gay


treatment=rep(NA,n)
treatment[pete.neutr==1]="p.neutral"
treatment[pete.gay==1]="p.gay"
treatment[pete.relig==1]="p.relig"
treatment[pete.milit==1]="p.milit"
treatment[pete.proudgay==1]="p.proudgay"
treatment[pete.milit.gay==1]="p.milit.gay"
treatment[pete.relig.gay==1]="p.relig.gay"
table(treatment)
pete.trump$treatment <- treatment

treatment.factor <- factor(treatment, levels = c("p.neutral", "p.gay", "p.proudgay", "p.milit", "p.milit.gay", "p.relig", "p.relig.gay"))

pete.trump$treatment.factor <- treatment.factor


treatment.factor.gaybase <- factor(treatment, levels = c("p.gay", "p.neutral", "p.proudgay", "p.milit", "p.milit.gay", "p.relig", "p.relig.gay"))
pete.trump$treatment.factor.gaybase <- treatment.factor.gaybase



##############


## CODE DVs


## PETE VS. TRUMP

# Vote choice
prop.table(table(voteChoice))

vote.pete <- as.numeric(voteChoice=="Pete Buttigieg")
table(vote.pete)
prop.table(table(vote.pete))

vote.trump <- as.numeric(voteChoice=="Donald Trump")
table(vote.trump)
prop.table(table(vote.trump))

vote.undec <- as.numeric(voteChoice=="Undecided")
table(vote.undec)
prop.table(table(vote.undec))

vote.pete.noundec = rep(NA,n) #includes only pete (=1) and trump (=0)
vote.pete.noundec[voteChoice=="Pete Buttigieg"]=1
vote.pete.noundec[voteChoice=="Donald Trump"]=0
table(vote.pete.noundec)
prop.table(table(vote.pete.noundec))


vote.pete.primary <- as.numeric(dem.primary=="Buttigieg")
table(vote.pete.primary)
prop.table(table(vote.pete.primary))




## 

#Liking Pete Buttigieg 
# "On a scale from 1 to 10, how much do you like Pete Buttigieg as a candidate?"
table(PeteLike_1)
like.pete <- PeteLike_1
table(like.pete)

#Voting for gay candidates - control
#"On a scale from 1 to 10, how comfortable are you in voting for a gay candidate?"
table(SocPress.C_1)
addmargins(table(party.ID,SocPress.C_1), 2)
comf.gaycand.cont<-SocPress.C_1
table(comf.gaycand.cont)



## Controls

table(educ)
educ.us = rep(NA,n)
educ.us[educ=="No high school"]=0
educ.us[educ=="Some high school but no diploma"]=1
educ.us[educ=="High school graduate"]=2
educ.us[educ=="Some college but no degree"]=3
educ.us[educ=="Associate degree in college"]=4
educ.us[educ=="Bachelor's degree"]=5
educ.us[educ=="Master's degree, professional school degree (for example: md, jd), or PhD"]=6
table(educ.us)
prop.table((table(educ.us)))


table(gender)
gender.us = rep(NA,n)
gender.us[gender=="Female"]="Female"
gender.us[gender=="Male"]="Male"
gender.us[gender=="Transgender female" | gender=="Transgender male"]="Trans"
gender.us[gender=="Other"]="Other"
table(gender.us)
prop.table(table(gender.us))

gender.us.factor <- factor(gender.us, levels = c("Male", "Female", "Trans", "Other"))
table(gender.us.factor)


table(gender)
female.us = rep(NA,n)
female.us[gender=="Female"]=1
female.us[gender=="Male"]=0
table(female.us)

table(gender)
female.us2 = rep(NA,n)
female.us2[gender=="Female"]="Female"
female.us2[gender=="Male"]="Male"
table(female.us2)


table(partyID)
party.ID = rep(NA,n)
party.ID[partyID=="Democrat"]="Democrat"
party.ID[partyID=="Independent"]="Independent"
party.ID[partyID=="Republican"]="Republican"
table(party.ID)
prop.table(table(party.ID))

party.ID.factor <- factor(party.ID, levels = c("Democrat", "Republican", "Independent"))
table(party.ID.factor)




table(income)
income.h.us = rep(NA,n)
income.h.us[income=="No income"]=0
income.h.us[income=="Less than  $20,000"]=1
income.h.us[income=="$20,000-$34,999"]=2
income.h.us[income=="$35,000-$49,999"]=3
income.h.us[income=="$50,000-$74,999"]=4
income.h.us[income=="$75,000-$99,999"]=5
income.h.us[income=="$100,000-$149,999"]=6
income.h.us[income=="$150,000-$199,999"]=7
income.h.us[income=="More than $200,000"]=8
table(income.h.us)
prop.table(table(income.h.us))

income.factor <- factor(income, levels=c("No income", "Less than  $20,000", "$20,000-$34,999", "$35,000-$49,999", 
                                         "$50,000-$74,999", "$75,000-$99,999", "$100,000-$149,999", "$150,000-$199,999",
                                         "More than $200,000"))


table(ideol)
conserv = rep(NA,n)
conserv[ideol=="Extremely liberal"]=1
conserv[ideol=="Liberal"]=2
conserv[ideol=="Slightly liberal"]=3
conserv[ideol=="Moderate or middle of the road"]=4
conserv[ideol=="Slightly conservative"]=5
conserv[ideol=="Conservative"]=6
conserv[ideol=="Extremely conservative"]=7
table(conserv)
prop.table(table(conserv))


table(LGBT.friends)
know.lgbt.yes = rep(NA,n)
know.lgbt.yes[LGBT.friends=="Yes"]=1
know.lgbt.yes[LGBT.friends=="No"]=0
table(know.lgbt.yes)

table(Sex.or)
sexual.or = rep(NA,n)
sexual.or[Sex.or=="Bisexual/pansexual"]="Bi/pan"
sexual.or[gender == "Female" & (Sex.or == "Gay" | Sex.or == "Lesbian")]="lesbian"
sexual.or[Sex.or == "Gay" & gender == "Male"]="gay man"
sexual.or[Sex.or=="Other"]="other"
sexual.or[Sex.or=="Straight"]="straight"
table(sexual.or)

sexual.or.factor <- factor(sexual.or, levels = c("straight", "gay man", "lesbian", "Bi/pan", "other"))

table(Sex.or)
lgbt.self = rep(NA,n)
lgbt.self[Sex.or == "Gay" | Sex.or == "Lesbian" | Sex.or == "Bisexual/pansexual" | Sex.or == "Other"
          | (gender == "Transgender female" & Sex.or == "Straight") | (gender == "Transgender male" & Sex.or == "Straight")
          | (gender == "Other" & Sex.or == "Straight")]=1
lgbt.self[Sex.or=="Straight"]=0
table(lgbt.self)
prop.table(table(lgbt.self))


table(relig.Y)
religious.yes = rep(NA,n)
religious.yes[relig.Y=="Yes"]=1
religious.yes[relig.Y=="No"]=0
table(religious.yes)


table(church)
religiosity = rep(NA,n)
religiosity[church=="Never"]=0
religiosity[church=="Only on special holidays"]=1
religiosity[church=="At least once a month"]=2
religiosity[church=="Once a week"]=3
religiosity[church=="Every day"]=4
table(religiosity)

highly.religious <- as.numeric(religiosity==3 | religiosity==4)
table(highly.religious)


table(race)
race.us = rep(NA,n)
race.us[race=="White"]="white"
race.us[race=="Black or African American" | race=="Black or African American,Other" | race=="Black or African American,White"]="black"
race.us[race=="Hispanic/Latino" | race=="Hispanic/Latino,Other" | race=="Hispanic/Latino,White"]="latinx"
race.us[race=="Asian" | race=="Asian,Other" | race=="Asian,White" | race=="Asian,White,Other"]="asian"
race.us[race=="American Indian or Alaska Native" | race=="Native Hawaiian or Pacific Islander" | race=="American Indian or Alaska Native,White" |
          race=="Native Hawaiian or Pacific Islander,White" | race=="American Indian or Alaska Native,White,Other"]="native"
race.us[race=="Other" | race=="American Indian or Alaska Native,Asian" | race=="American Indian or Alaska Native,Asian,Black or African American" | 
          race=="American Indian or Alaska Native,Asian,Black or African American,Hispanic/Latino,White" | 
          race=="American Indian or Alaska Native,Asian,Black or African American,Native Hawaiian or Pacific Islander,White" | 
          race=="American Indian or Alaska Native,Asian,Hispanic/Latino,White" | race=="American Indian or Alaska Native,Asian,White" | 
          race=="American Indian or Alaska Native,Black or African American" | race=="American Indian or Alaska Native,Black or African American,Hispanic/Latino" | 
          race=="American Indian or Alaska Native,Black or African American,Hispanic/Latino,White" | 
          race=="American Indian or Alaska Native,Black or African American,White" | race=="Other" | 
          race=="American Indian or Alaska Native,Black or African American,White,Other" | race=="American Indian or Alaska Native,Hispanic/Latino" | 
          race=="American Indian or Alaska Native,Hispanic/Latino,White" |  race=="Asian,Black or African American" | race=="Asian,Hispanic/Latino" | 
          race=="Asian,Hispanic/Latino,White" | race=="Asian,Native Hawaiian or Pacific Islander,White" | race=="Black or African American,Hispanic/Latino" | 
          race=="Black or African American,Hispanic/Latino,White" | race=="Black or African American,Native Hawaiian or Pacific Islander" | 
          race=="Black or African American,Native Hawaiian or Pacific Islander,White" | race=="Hispanic/Latino,Native Hawaiian or Pacific Islander" | 
          race=="White,Other"]="other"
table(race.us)
prop.table(table(race.us))

race.us.factor <- factor(race.us, levels = c("white", "black", "latinx", "asian", "native", "other"))
table(race.us.factor)


table(age)
age.years<-2020-age
table(age.years)
mean(age.years, na.rm = T)
median(age.years, na.rm = T)



table(state)
california <- as.numeric(state=="California")
colorado <- as.numeric(state=="Colorado")
florida <- as.numeric(state=="Florida")
indiana <- as.numeric(state=="Indiana")
michigan <- as.numeric(state=="Michigan")
minnesota <- as.numeric(state=="Minnesota")
newyork <- as.numeric(state=="New York")
northcarolina <- as.numeric(state=="North Carolina")
ohio <- as.numeric(state=="Ohio")
pennsylvania <- as.numeric(state=="Pennsylvania")
texas <- as.numeric(state=="Texas")
viriginia <- as.numeric(state=="Virginia")

state.factor <- factor(state, levels=c("California", "Colorado", "Florida", "Indiana", "Michigan", "Minnesota",
                                       "New York", "North Carolina", "Ohio", "Pennsylvania", "Texas", "Virginia"))



###################################
##################################




## ANALYSIS




## FIGURE 1 MANUSCRIPT

## TREATMENT EFFECT: COMPARISON OF PROPORTIONS



#proportions
prop.table(table(voteChoice, treatment),2)
t<-prop.table(table(voteChoice, treatment),2)
write.table(format(t*100, digits=3), file = "treateffects.txt", sep = ",", quote = FALSE, row.names = F)


#### ATTENTION: USE THIS JUST ABOVE ALSO FOR TABLE 3 MANUSCRIPT BUT SELECTING THE RIGHT SUBSET AMONG THE ONES AT THE TOP OF THE CODE ###




# TABLE 1 MANUSCRIPT AND TABLE A1 APPENDIX

# OLS MODELS

# Treatment factor: baseline: Mayor Pete


# OLS individual controls
vote.pete.ols.controls <- lm(vote.pete~treatment.factor + age.years + gender.us.factor + educ.us + race.us.factor + party.ID)
summary(vote.pete.ols.controls)

# OLS individual controls + state
vote.pete.ols.controls.state <- lm(vote.pete~treatment.factor + age.years + gender.us.factor + educ.us + race.us.factor + party.ID + state)
summary(vote.pete.ols.controls.state)

library(stargazer)
stargazer(vote.pete.ols.controls, vote.pete.ols.controls.state, type="html", digits = 2,
          star.char = c("+", "*", "**", "***"), 
          star.cutoffs = c(0.1, 0.05, 0.01, 0.001), 
          notes        = "+p<0.1; *p<0.05; **p<0.01; ***p<0.001", 
          notes.append = FALSE, no.space = TRUE,
          out="mod.htm")



# TABLE 2 MANUSCRIPT: Support for Pete Buttigieg on a 10-point scale


# OLS like pete individual controls + state
like.pete.ols.controls.state <- lm(like.pete~treatment.factor + age.years + gender.us.factor + educ.us + race.us.factor + party.ID + state)
summary(like.pete.ols.controls.state)

stargazer(like.pete.ols.controls.state, type="html", digits = 2,
          star.char = c("+", "*", "**", "***"), 
          star.cutoffs = c(0.1, 0.05, 0.01, 0.001), 
          notes        = "+p<0.1; *p<0.05; **p<0.01; ***p<0.001", 
          notes.append = FALSE, no.space = TRUE,
          out="mod.htm")


# TABLE A2 APPENDIX

## MULTILEVEL MODELS


library(lme4)
vote.pete.multilevel <- lmer(vote.pete~treatment.factor + age.years + gender.us.factor + educ.us + race.us.factor + party.ID + (1| state))
summary(vote.pete.multilevel)

stargazer(vote.pete.multilevel, type="html", digits = 2,
          star.char = c("+", "*", "**", "***"), 
          star.cutoffs = c(0.1, 0.05, 0.01, 0.001), 
          notes        = "+p<0.1; *p<0.05; **p<0.01; ***p<0.001", 
          notes.append = FALSE, no.space = TRUE,
          out="mod.htm")





# TABLE A3 APPENDIX

# OLS MODELS

# Treatment factor: baseline: gay Pete


# OLS individual controls
vote.pete.ols.controls.b <- lm(vote.pete~treatment.factor.gaybase + age.years + gender.us.factor + educ.us + race.us.factor + party.ID)
summary(vote.pete.ols.controls.b)

# OLS individual controls + state
vote.pete.ols.controls.state.b <- lm(vote.pete~treatment.factor.gaybase + age.years + gender.us.factor + educ.us + race.us.factor + party.ID + state)
summary(vote.pete.ols.controls.state.b)

stargazer(vote.pete.ols.controls.b, vote.pete.ols.controls.state.b, type="html", digits = 2,
          star.char = c("+", "*", "**", "***"), 
          star.cutoffs = c(0.1, 0.05, 0.01, 0.001), 
          notes        = "+p<0.1; *p<0.05; **p<0.01; ***p<0.001", 
          notes.append = FALSE, no.space = TRUE,
          out="mod.htm")




# TABLE A4 APPENDIX

## MULTINOMIAL MODELS

library(nnet)

table(voteChoice)
n = nrow(pete.trump)
vote.choice = rep(NA, n)
vote.choice[voteChoice=="Pete Buttigieg"]="Buttigieg"
vote.choice[voteChoice=="Donald Trump"]="Trump"
vote.choice[voteChoice=="Undecided"]="Undecided"
table(vote.choice)



# Multinomial individual controls
vote.multinomial.controls <- multinom(vote.choice~treatment.factor + age.years + gender.us.factor + educ.us + race.us.factor + party.ID)
summary(vote.multinomial.controls)

# Multinomial individual controls + state
vote.multinomial.controls.state <- multinom(vote.choice~treatment.factor + age.years + gender.us.factor + educ.us + race.us.factor + party.ID + state)
summary(vote.multinomial.controls.state)

stargazer(vote.multinomial.controls, vote.multinomial.controls.state, type="html", 
          star.char = c("+", "*", "**", "***"), 
          star.cutoffs = c(0.1, 0.05, 0.01, 0.001), 
          notes        = "+p<0.1; *p<0.05; **p<0.01; ***p<0.001", 
          notes.append = FALSE, no.space = TRUE,
          out="mod.htm")




# TABLE A5 APPENDIX

## Like Pete

table(like.pete)

# OLS like pete individual controls
like.pete.ols.controls <- lm(like.pete~treatment.factor + age.years + gender.us.factor + educ.us + race.us.factor + party.ID)
summary(like.pete.ols.controls)

# OLS like pete individual controls + state
like.pete.ols.controls.state <- lm(like.pete~treatment.factor + age.years + gender.us.factor + educ.us + race.us.factor + party.ID + state)
summary(like.pete.ols.controls.state)

stargazer(like.pete.ols.controls, like.pete.ols.controls.state, type="html", digits = 2,
          star.char = c("+", "*", "**", "***"), 
          star.cutoffs = c(0.1, 0.05, 0.01, 0.001), 
          notes        = "+p<0.1; *p<0.05; **p<0.01; ***p<0.001", 
          notes.append = FALSE, no.space = TRUE,
          out="mod.htm")



# TABLE A6 APPENDIX

## Like Pete - with BASELINE GAY PETE


# OLS like pete individual controls - with BASELINE GAY PETE
like.pete.ols.controls.b <- lm(like.pete~treatment.factor.gaybase + age.years + gender.us.factor + educ.us + race.us.factor + party.ID)
summary(like.pete.ols.controls.b)

# OLS individual controls + state - with BASELINE GAY PETE
like.pete.ols.controls.state.b <- lm(like.pete~treatment.factor.gaybase + age.years + gender.us.factor + educ.us + race.us.factor + party.ID + state)
summary(like.pete.ols.controls.state.b)

stargazer(like.pete.ols.controls.b, like.pete.ols.controls.state.b, type="html", digits = 2,
          star.char = c("+", "*", "**", "***"), 
          star.cutoffs = c(0.1, 0.05, 0.01, 0.001), 
          notes        = "+p<0.1; *p<0.05; **p<0.01; ***p<0.001", 
          notes.append = FALSE, no.space = TRUE,
          out="mod.htm")




# TABLE A7 APPENDIX

# MULTIPLE PROPORTION COMPARISON

# Vote.Pete as DV (% voting for Pete, separate from Trump and undecided)

#Prop.test
vote.peteTab <- with(pete.trump, table(treatment.factor, vote.pete))
prop.test(vote.peteTab)

#Pairwise prop.test - two-tailed
pairwise.prop.test(x = vote.peteTab, p.adjust.method = "none")

## ATTENTION! USE SUBSET AT THE TOP OF THE CODE FOR SUBGROUPS OF VOTERS




# TABLE A8 APPENDIX: Voting for Pete Buttigieg: before and after he dropped out of the primaries

# ATTENTION! USE RIGHT SUBSET

## Subsets 
## Only needed for TABLE A8 - otherwise skip this section

## before Pete DROPPED out (i.e. respondents completing survey before 6 pm on March 1 - N = 4,894)
pete.trump <- read.csv("C:/Users/gmagni/OneDrive/SideProjects/9-Andy-Pete/00-Results/Vote_choicePete_March 5, 2020_7pm-numbers-BEFOREpeteDroppedOut.csv")

## AFTER Pete DROPPED out (i.e. respondents completing survey before 6 pm on March 1 - N = 4,894)
pete.trump <- read.csv("C:/Users/gmagni/OneDrive/SideProjects/9-Andy-Pete/00-Results/Vote_choicePete_March 5, 2020_7pm-numbers-AFTERpeteDroppedOut.csv")



#proportions
prop.table(table(voteChoice, treatment),2)
t<-prop.table(table(voteChoice, treatment),2)
write.table(format(t*100, digits=3), file = "treateffects.txt", sep = ",", quote = FALSE, row.names = F)



# TABLE A9 APPENDIX: Voting for Pete Buttigieg: OLS models (before he dropped out of the primaries)

# ATTENTION! USE RIGHT SUBSET

## Subsets 
## Only needed for TABLE A8 - otherwise skip this section

## before Pete DROPPED out (i.e. respondents completing survey before 6 pm on March 1 - N = 4,894)
pete.trump <- read.csv("C:/Users/gmagni/OneDrive/SideProjects/9-Andy-Pete/00-Results/Vote_choicePete_March 5, 2020_7pm-numbers-BEFOREpeteDroppedOut.csv")

## AFTER Pete DROPPED out (i.e. respondents completing survey before 6 pm on March 1 - N = 4,894)
pete.trump <- read.csv("C:/Users/gmagni/OneDrive/SideProjects/9-Andy-Pete/00-Results/Vote_choicePete_March 5, 2020_7pm-numbers-AFTERpeteDroppedOut.csv")



# OLS individual controls + state
vote.pete.ols.controls.state <- lm(vote.pete~treatment.factor + age.years + gender.us.factor + educ.us + race.us.factor + party.ID + state)
summary(vote.pete.ols.controls.state)

library(stargazer)
stargazer(vote.pete.ols.controls.state, type="html", digits = 2,
          star.char = c("+", "*", "**", "***"), 
          star.cutoffs = c(0.1, 0.05, 0.01, 0.001), 
          notes        = "+p<0.1; *p<0.05; **p<0.01; ***p<0.001", 
          notes.append = FALSE, no.space = TRUE,
          out="mod.htm")



# TABLE A1O APPENDIX: Support for Buttigieg (liking Buttigieg on a 10-point scale): OLS models (before he dropped out of the primaries)

# ATTENTION! USE RIGHT SUBSET

## Subsets 
## Only needed for TABLE A8 - otherwise skip this section

## before Pete DROPPED out (i.e. respondents completing survey before 6 pm on March 1 - N = 4,894)
pete.trump <- read.csv("C:/Users/gmagni/OneDrive/SideProjects/9-Andy-Pete/00-Results/Vote_choicePete_March 5, 2020_7pm-numbers-BEFOREpeteDroppedOut.csv")

## AFTER Pete DROPPED out (i.e. respondents completing survey before 6 pm on March 1 - N = 4,894)
pete.trump <- read.csv("C:/Users/gmagni/OneDrive/SideProjects/9-Andy-Pete/00-Results/Vote_choicePete_March 5, 2020_7pm-numbers-AFTERpeteDroppedOut.csv")


# OLS like pete individual controls + state
like.pete.ols.controls.state <- lm(like.pete~treatment.factor + age.years + gender.us.factor + educ.us + race.us.factor + party.ID + state)
summary(like.pete.ols.controls.state)

library(stargazer)
stargazer(like.pete.ols.controls.state, type="html", digits = 2,
          star.char = c("+", "*", "**", "***"), 
          star.cutoffs = c(0.1, 0.05, 0.01, 0.001), 
          notes        = "+p<0.1; *p<0.05; **p<0.01; ***p<0.001", 
          notes.append = FALSE, no.space = TRUE,
          out="mod.htm")





# FIGURE A1 APPENDIX


### randomization checks


#value distributions across treatments
pete.trump$age.years <- age.years
ddply(pete.trump, .(treatment), summarize,  mean=mean(age.years, na.rm=TRUE))

pete.trump$female.us <- female.us
ddply(pete.trump, .(treatment), summarize,  mean=mean(female.us, na.rm=TRUE))

pete.trump$whites <- whites
ddply(pete.trump, .(treatment), summarize,  mean=mean(whites, na.rm=TRUE))

pete.trump$educ.us <- educ.us
ddply(pete.trump, .(treatment), summarize,  mean=mean(educ.us, na.rm=TRUE))

pete.trump$income.h.us <- income.h.us
ddply(pete.trump, .(treatment), summarize,  mean=mean(income.h.us, na.rm=TRUE))

pete.trump$democr <- democr
ddply(pete.trump, .(treatment), summarize,  mean=mean(democr, na.rm=TRUE))

pete.trump$conserv <- conserv
ddply(pete.trump, .(treatment), summarize,  mean=mean(conserv, na.rm=TRUE))

pete.trump$religiosity <- religiosity
ddply(pete.trump, .(treatment), summarize,  mean=mean(religiosity, na.rm=TRUE))

pete.trump$know.lgbt.yes <- know.lgbt.yes
ddply(pete.trump, .(treatment), summarize,  mean=mean(know.lgbt.yes, na.rm=TRUE))



#Tukey's Honestly Significant Difference test

#age
model.age <- aov(age.years ~ treatment, data = pete.trump)
tukey_results.age <- TukeyHSD(model.age)
print(tukey_results.age)
plot(tukey_results.age) + par(las = 1, mar = c(3, 10, 3, 5), cex.lab = 1, cex.axis = 0.8) 
plot(tukey_results.age) + title("\n Tukey's Honestly Significant Difference test: age")

#gender: % women
model.gender <- aov(female.us ~ treatment, data = pete.trump)
tukey_results.gender <- TukeyHSD(model.gender)
print(tukey_results.gender)
plot(tukey_results.gender) + par(las = 1, mar = c(3, 10, 3, 5), cex.lab = 1, cex.axis = 0.8) 
plot(tukey_results.gender) + title("\n Tukey's Honestly Significant Difference test: gender (% women)")

#race: % white
model.race <- aov(whites ~ treatment, data = pete.trump)
tukey_results.race <- TukeyHSD(model.race)
print(tukey_results.race)
plot(tukey_results.race) + par(las = 1, mar = c(3, 10, 3, 5), cex.lab = 1, cex.axis = 0.8) 
plot(tukey_results.race) + title("\n Tukey's Honestly Significant Difference test: race (% whites)")

#education
model.educ <- aov(educ.us ~ treatment, data = pete.trump)
tukey_results.educ <- TukeyHSD(model.educ)
print(tukey_results.educ)
plot(tukey_results.educ) + par(las = 1, mar = c(3, 10, 3, 5), cex.lab = 1, cex.axis = 0.8) 
plot(tukey_results.educ) + title("\n Tukey's Honestly Significant Difference test: education")

#income
model.income <- aov(income.h.us ~ treatment, data = pete.trump)
tukey_results.income <- TukeyHSD(model.income)
print(tukey_results.income)
plot(tukey_results.income) + par(las = 1, mar = c(3, 10, 3, 5), cex.lab = 1, cex.axis = 0.8) 
plot(tukey_results.income) + title("\n Tukey's Honestly Significant Difference test: income")

#party ID: % democrats
model.party <- aov(democr ~ treatment, data = pete.trump)
tukey_results.party <- TukeyHSD(model.party)
print(tukey_results.party)
plot(tukey_results.party) + par(las = 1, mar = c(3, 10, 3, 5), cex.lab = 1, cex.axis = 0.8) 
plot(tukey_results.party) + title("\n Tukey's Honestly Significant Difference test: party ID (% Democrats)")

#ideology: conservatives
model.conservat <- aov(conserv ~ treatment, data = pete.trump)
tukey_results.conservat <- TukeyHSD(model.conservat)
print(tukey_results.conservat)
plot(tukey_results.conservat) + par(las = 1, mar = c(3, 10, 3, 5), cex.lab = 1, cex.axis = 0.8) 
plot(tukey_results.conservat) + title("\n Tukey's Honestly Significant Difference test: ideology - conservatives")

#religiosity
model.relig <- aov(religiosity ~ treatment, data = pete.trump)
tukey_results.relig <- TukeyHSD(model.relig)
print(tukey_results.relig)
plot(tukey_results.relig) + par(las = 1, mar = c(3, 10, 3, 5), cex.lab = 1, cex.axis = 0.8) 
plot(tukey_results.relig) + title("\n Tukey's Honestly Significant Difference test: religiosity")

#knowing LGBT people
model.knowLGBT <- aov(know.lgbt.yes ~ treatment, data = pete.trump)
tukey_results.knowLGBT <- TukeyHSD(model.knowLGBT)
print(tukey_results.knowLGBT)
plot(tukey_results.knowLGBT) + par(las = 1, mar = c(3, 10, 3, 5), cex.lab = 1, cex.axis = 0.8) 
plot(tukey_results.knowLGBT) + title("\n Tukey's Honestly Significant Difference test: LGBTQ friends")




## ANALYSIS OPEN-ENDED ANSWERS ##


# Analysis for TABLE A11 and A12 APPENDIX- Manipulation checks ("Mayor Pete" baseline condition and "Gay Pete" baseline condition)


# code reasons for like / dislike

p.pos.general=rep(NA,n)
p.pos.general <- PetePositiveGeneral
table(p.pos.general)

p.pos.gay=rep(NA,n)
p.pos.gay <- PetePositiveGay

p.pos.young=rep(NA,n)
p.pos.young <- PetePositiveYoung

p.pos.milit=rep(NA,n)
p.pos.milit <- PetePositiveMilitary

p.pos.relig=rep(NA,n)
p.pos.relig <- PetePositiveReligious

p.pos.nottrump=rep(NA,n)
p.pos.nottrump <- PetePositiveNotTrump

p.pos.empathy=rep(NA,n)
p.pos.empathy <- PetePositiveEmpathy

p.pos.moder=rep(NA,n)
p.pos.moder <- PetePositiveModerate

p.pos.liber=rep(NA,n)
p.pos.liber <- PetePositiveLiberal

p.pos.democr=rep(NA,n)
p.pos.democr <- PetePositiveDemocrat

p.pos.other=rep(NA,n)
p.pos.other <- PetePositiveOther



p.neg.general = rep(NA,n)
p.neg.general <- PeteNegativeGeneral

p.neg.gay.homoph = rep(NA,n)
p.neg.gay.homoph <- PeteNegativeGayHomoph

p.neg.young = rep(NA,n)
p.neg.young <- PeteNegativeYoung

p.neg.milit = rep(NA,n)
p.neg.milit <- PeteNegativeMilitary

p.neg.relig = rep(NA,n)
p.neg.relig <- PeteNegativeReligious

p.neg.nottrump = rep(NA,n)
p.neg.nottrump <- PeteNegativeNotTrump

p.neg.gay.elect = rep(NA,n)
p.neg.gay.elect <- PeteNegativeGayElectab

p.neg.too.moder = rep(NA,n)
p.neg.too.moder <- PeteNegativeTooModer

p.neg.too.liber = rep(NA,n)
p.neg.too.liber <- PeteNegativeTooLiber

p.neg.democr = rep(NA,n)
p.neg.democr <- PeteNegativeDemocrat

p.neg.other = rep(NA,n)
p.neg.other <- PeteNegativeOther


p.gay.pos.or.negat = rep(NA,n)
p.gay.pos.or.negat <- as.numeric(p.pos.gay == 1 | p.neg.gay.homoph ==1)
table(p.pos.gay)
table(p.neg.gay.homoph)
table(p.gay.pos.or.negat)

p.milit.pos.or.negat = rep(NA,n)
p.milit.pos.or.negat <- as.numeric(p.pos.milit | p.neg.milit)

p.relig.pos.or.negat = rep(NA,n)
p.relig.pos.or.negat <- as.numeric(p.pos.relig | p.neg.relig)




## Manipulation checks for treatment

# TABLE A11 APPENDIX

check.gay.treat <- lm(p.gay.pos.or.negat~treatment.factor)
summary(check.gay.treat)

check.milit.treat <- lm(p.milit.pos.or.negat~treatment.factor)
summary(check.milit.treat)

check.relig.treat <- lm(p.relig.pos.or.negat~treatment.factor)
summary(check.relig.treat)

library(stargazer)
stargazer(check.gay.treat, check.milit.treat, check.relig.treat, type="html", digits = 2,
          star.char = c("*", "**", "***"), 
          star.cutoffs = c(0.05, 0.01, 0.001), 
          notes        = "*p<0.05; **p<0.01; ***p<0.001", 
          notes.append = FALSE, no.space = TRUE,
          out="mod.htm")


# TABLE A12 APPENDIX

check.gay.treat.2 <- lm(p.gay.pos.or.negat~treatment.factor.gaybase)
summary(check.gay.treat.2)

stargazer(check.gay.treat.2, type="html", digits = 2,
          star.char = c("*", "**", "***"), 
          star.cutoffs = c(0.05, 0.01, 0.001), 
          notes        = "*p<0.05; **p<0.01; ***p<0.001", 
          notes.append = FALSE, no.space = TRUE,
          out="mod.htm")


